﻿Imports System.Collections.Specialized

Public Class frmTabMiscellaneous
    Dim editNode As EditXML = New EditXML()
    Public Shared sPdelim As String = "£"

    Public Shared sBrowseGraphics As StringCollection = New StringCollection()
    Public Shared XRefBase As Collection = New Collection()
    Public Shared bXRefLWC As Collection = New Collection()
    Public Shared XRefLarge As Collection = New Collection()

    Private Sub lboxBrGraphic_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxBrGraphic.SelectedIndexChanged
        If lboxBrGraphic.SelectedItems.Count() > 0 Then
            btnGraphicEdit.Enabled = True
            btnGraphicDelete.Enabled = True
        Else
            btnGraphicEdit.Enabled = False
            btnGraphicDelete.Enabled = False
        End If
    End Sub

    Private Sub lboxXRef_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxXRef.SelectedIndexChanged
        If lboxXRef.SelectedItems.Count() > 0 Then
            btnXRefEdit.Enabled = True
            btnXRefDelete.Enabled = True
        Else
            btnXRefEdit.Enabled = False
            btnXRefDelete.Enabled = False
        End If
    End Sub

    Private Sub btnGraphicAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGraphicAdd.Click
        Dim sGenArray() As String
        Dim s1 As String
        Dim frmAddBG As New frmBrowseGraphic

        frmAddBG.ShowDialog()
        If (frmAddBG.getUpdated() = True) Then
            s1 = frmAddBG.getBrowseG()
            sBrowseGraphics.Add(s1)
            sGenArray = Split(s1, sPdelim)
            s1 = sGenArray(0) & "    " & sGenArray(1)
            lboxBrGraphic.Items.Add(s1)
        End If
    End Sub

    Private Sub btnGraphicEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGraphicEdit.Click
        Dim iCount As Integer = lboxBrGraphic.Items.Count
        Dim iSelInd As Integer = lboxBrGraphic.SelectedIndex
        If (iCount = 1) Then iSelInd = 0
        If (iSelInd = -1) Then 'no index selected
            MessageBox.Show("No graphic selected for editing", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        Dim sGenArray() As String
        Dim s1 As String = sBrowseGraphics(iSelInd) 'get info on selected browse graphic
        Dim frmEditBG As New frmBrowseGraphic(s1)   'and send to editing window

        frmEditBG.ShowDialog()
        If (frmEditBG.getUpdated() = True) Then
            sBrowseGraphics.Insert(iSelInd, frmEditBG.getBrowseG()) 'put edited browse graphic into collection
            sGenArray = Split(frmEditBG.getBrowseG(), sPdelim)      'create entry to insert into display list
            s1 = sGenArray(0) & "    " & sGenArray(1)
            lboxBrGraphic.Items.Insert(iSelInd, s1)
            sBrowseGraphics.RemoveAt(iSelInd + 1) 'remove old version of graphic from collection
            lboxBrGraphic.Items.RemoveAt(iSelInd + 1) 'remove old version of graphic from displayed list
        End If
        btnGraphicEdit.Enabled = False
        btnGraphicDelete.Enabled = False
    End Sub

    Private Sub btnGraphicDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGraphicDelete.Click
        Dim iSelInd As Integer = 0

        iSelInd = lboxBrGraphic.SelectedIndex
        If (iSelInd = -1) Then 'no index selected
            MessageBox.Show("No graphic selected for deletion", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        sBrowseGraphics.RemoveAt(iSelInd) 'remove selected graphic from collection
        lboxBrGraphic.Items.RemoveAt(iSelInd) 'remove selected graphic from displayed list
        btnGraphicEdit.Enabled = False
        btnGraphicDelete.Enabled = False
    End Sub

    Private Sub btnXRefAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXRefAdd.Click
        Dim tmpCite As New CiteInfo
        Dim tmpLWC As New CiteInfo
        Dim tmpLWCb As Boolean = False
        Dim strCite As String = ""
        Dim iCount As Integer = 0

        Dim frmXRef As New frmCiteInfo(tmpCite, tmpLWC, tmpLWCb, frmMain.getTimeStyle())
        frmXRef.ShowDialog()

        If (frmXRef.getUpdated() = False) Then  'no citation was provided so no updating
            Return
        End If
        tmpCite = frmXRef.getNewCite()
        tmpLWCb = frmXRef.getLWC()
        If (tmpLWCb = True) Then
            tmpLWC = frmXRef.getNewLWC()
        End If

        XRefBase.Add(tmpCite) 'add new citation to the collection
        bXRefLWC.Add(tmpLWCb) 'add new larger work Boolean to its collection
        XRefLarge.Add(tmpLWC) 'add new larger work citation to its collection

        strCite = Microsoft.VisualBasic.Left(tmpCite.Author, 25) & "   " & Microsoft.VisualBasic.Left(tmpCite.Title, 25)
        lboxXRef.Items.Add(strCite)
    End Sub

    Private Sub btnXRefEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXRefEdit.Click
        Dim iCount As Integer = lboxXRef.Items.Count
        Dim iSelInd As Integer = lboxXRef.SelectedIndex
        If (iCount = 1) Then iSelInd = 0
        If (iSelInd = -1) Then 'no index selected; zero-based
            MessageBox.Show("No cross-reference selected for editing", "Cross-reference selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        Dim tmpCite As CiteInfo = CType(XRefBase(iSelInd + 1), CiteInfo) 'get base citation from collection; one-based
        Dim tmpLWC As CiteInfo = CType(XRefLarge(iSelInd + 1), CiteInfo) 'get larger work citation from collection; one-based
        Dim tmpLWCb As Boolean = CType(bXRefLWC(iSelInd + 1), Boolean) 'get Boolean for LWC from collection; one-based
        Dim strCite As String = ""

        Dim frmXRefEdit As New frmCiteInfo(tmpCite, tmpLWC, tmpLWCb, frmMain.getTimeStyle())
        frmXRefEdit.ShowDialog()

        If (frmXRefEdit.getUpdated() = False) Then  'no citation was provided so no updating
            Return
        End If
        tmpCite = frmXRefEdit.getNewCite()
        tmpLWCb = frmXRefEdit.getLWC()
        If (tmpLWCb = True) Then
            tmpLWC = frmXRefEdit.getNewLWC()
        End If

        XRefBase.Add(tmpCite, , iSelInd + 1) 'add edited citation to the collection
        bXRefLWC.Add(tmpLWCb, , iSelInd + 1) 'add edited larger work Boolean to its collection
        XRefLarge.Add(tmpLWC, , iSelInd + 1) 'add edited larger work citation to its collection

        XRefBase.Remove(iSelInd + 2) 'remove old citation to the collection
        bXRefLWC.Remove(iSelInd + 2) 'remove old larger work Boolean to its collection
        XRefLarge.Remove(iSelInd + 2) 'remove old larger work citation to its collection

        strCite = Microsoft.VisualBasic.Left(tmpCite.Author, 25) & "   " & Microsoft.VisualBasic.Left(tmpCite.Title, 25)
        lboxXRef.Items.Insert(iSelInd, strCite)
        lboxXRef.Items.RemoveAt(iSelInd + 1)
        btnXRefEdit.Enabled = False
        btnXRefDelete.Enabled = False
    End Sub

    Private Sub btnXRefDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXRefDelete.Click
        Dim iSelInd As Integer = 0

        iSelInd = lboxXRef.SelectedIndex
        If (iSelInd = -1) Then 'no index selected
            MessageBox.Show("No cross-reference selected for deletion", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        XRefBase.Remove(iSelInd + 1)  'remove main cross-reference citation collection (one-based)
        bXRefLWC.Remove(iSelInd + 1)  'remove associated Boolean for larger work citation
        XRefLarge.Remove(iSelInd + 1) 'remove entry for larger work citation
        lboxXRef.Items.RemoveAt(iSelInd) 'remove selected graphic from displayed list
        btnXRefEdit.Enabled = False
        btnXRefDelete.Enabled = False
    End Sub

    Private Sub txtDScredit_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDScredit.Leave
        editXMLFile()
    End Sub

    Private Sub txtDSenviron_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDSenviron.Leave
        editXMLFile()
    End Sub

    Private Sub frmTabMiscellaneous_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        editXMLFile()
    End Sub

    Public Sub editXMLFile()
        editNode.Remove("/metadata/idinfo", "browse")
        editNode.Remove("/metadata/idinfo", "crossref")
        editNode.Edit("/metadata/idinfo/datacred", txtDScredit.Text.Trim())
        editNode.Edit("/metadata/idinfo/native", txtDSenviron.Text.Trim())
        For i As Integer = XRefBase.Count() To 1 Step -1
            Dim newNode As String = ""
            newNode = newNode & editNode.createCiteInfo(XRefBase(i), bXRefLWC(i), XRefLarge(i))
            editNode.Add("/metadata/idinfo", "/native", "crossref", newNode)
        Next
        For i As Integer = sBrowseGraphics.Count() To 1 Step -1
            Dim items() As String = sBrowseGraphics(i - 1).Split(sPdelim)
            Dim newNode As String = ""
            newNode = newNode & "<browsen>" & items(0) & "</browsen><browsed>" & items(1) & "</browsed><browset>" & items(2) & "</browset>"
            editNode.Add("/metadata/idinfo", "/native", "browse", newNode)
        Next
    End Sub

    Private Sub button_example_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_example.Click
        Dim helpForm As dialog_help = New dialog_help("Miscellaneous")
        helpForm.ShowDialog()
    End Sub
End Class